package study.web.blog;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import study.domain.user.User;
import study.service.blog.BlogService;
import study.service.user.UserService;
import study.service.web.SessionService;

@Controller
@RequestMapping(value = "/blog/{username}/{postId}/comments")
public class CommentController {

	@Resource(name = "blogService")
	private BlogService blogService;

	@Resource(name = "userService")
	private UserService userService;

	@Resource(name = "sessionService")
	private SessionService sessionService;

	@RequestMapping(value = "/create", method=RequestMethod.POST)
	public void create(@PathVariable("username") String username,
			@PathVariable("postId") Long postId, String contents) {
		User user = userService.getUserByUsername(username);
		if(user == null) {
			throw new RuntimeException("NOT FOUND user "+username);
		}
		User currentUser = sessionService.getCurrentUser();
		blogService.addComment(currentUser, postId, contents);
		//return "redirect:/blog/"+currentUser.getUsername();
	}
}
